package jsyf.oa.service.capital;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.constant.Const;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.capital.BankDebitsDao;
import jsyf.oa.entity.capital.BankDebitsEntity;
import jsyf.oa.facade.capital.BankDebitsFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by houtailei on 2018/2/25.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class BankDebitsService implements BankDebitsFacade {
    @Autowired
    private BankDebitsDao bankdebitsledgerDao;

    @Override
    public String save(String jsonData) {
        BankDebitsEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), BankDebitsEntity.class);
        Integer rows;
        if ("0".equals(entity.getId()) || StringUtils.isBlank(entity.getId())) {
            rows = bankdebitsledgerDao.add(entity);
        } else {
            rows = bankdebitsledgerDao.update(entity);
        }
        return rows == 0 ? "" : entity.getId();
    }

    @Override
    public JSONObject getById(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String id = jsonParams.getString("id");
        return bankdebitsledgerDao.getJSONById(id);
    }

    @Override
    public String delById(String jsonData) {
        Integer rows;
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String id = jsonParams.getString("id");
        String delReason = jsonParams.getString("delReason");
        BankDebitsEntity entity = (BankDebitsEntity) bankdebitsledgerDao.getIBaseEntityById(id);
        entity.setDelFlag(1);
        if (!StringUtils.isBlank(delReason)) {
            entity.setDelReason(delReason);
        }
        rows = bankdebitsledgerDao.update(entity);
        if (Const.INT_ONE == rows) {
            return BaseUtils.succsessObjRespond("退票成功");
        }
        return BaseUtils.errorrRespond("退票失败");
    }

    @Override
    public JSONObject getBankDebitsByCompanyId(String jsonData) {
        JSONObject result = new JSONObject();
        JSONObject sendData = new JSONObject();
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        String debitsNo = jsonParams.getString("debitsNo");
        //项目
        String comeProject = jsonParams.getString("comeProject");
        String dueDate = jsonParams.getString("dueDate");
        String endorseFlag = jsonParams.getString("endorseFlag");
        String endorseDate = jsonParams.getString("endorseDate");
        String key = jsonParams.getString("key");
        String requestPage = jsonParams.getString("requestPage");
        String pageSize = jsonParams.getString("pageSize");
        StringBuilder condition = new StringBuilder();
        List<Object> sqlArgs = new ArrayList<Object>();
        if (!StringUtils.isBlank(companyId)) {
            condition.append(" AND fkCompanyId=?");
            sqlArgs.add(companyId);
        }
        if (!StringUtils.isBlank(debitsNo)) {
            condition.append(" AND debitsNo=?");
            sqlArgs.add(debitsNo);
        }
        if (!StringUtils.isBlank(comeProject)) {
            condition.append(" and (incomeProject LIKE ?"
                    + " or outcomeProject LIKE ?)");
            sqlArgs.add("%" + comeProject + "%");
            sqlArgs.add("%" + comeProject + "%");
        }
        if (!StringUtils.isBlank(endorseFlag)) {
            condition.append(" AND endorseFlag=?");
            sqlArgs.add(endorseFlag);
        }
        if (!StringUtils.isBlank(dueDate)) {
            condition.append(" AND dueDate=?");
            sqlArgs.add(dueDate);
        }
        if (!StringUtils.isBlank(endorseDate)) {
            condition.append(" AND endorseDate=?");
            sqlArgs.add(endorseDate);
        }
        if (!StringUtils.isBlank(key)) {
            condition.append(" AND drawerName LIKE ?");
            sqlArgs.add("%" + key + "%");
        }
        condition.append(" AND delFlag=0");
        condition.append(" ORDER BY createTime DESC");
        List<JSONObject> jsonList = bankdebitsledgerDao.getViewJSONListByPage(condition.toString(), sqlArgs.toArray(),
                Integer.valueOf(requestPage), Integer.valueOf(pageSize));
        int recordCount = bankdebitsledgerDao.getViewPageCount(condition.toString(), sqlArgs.toArray());
        int recordSize = Integer.valueOf(pageSize).intValue();
        int pageCount = 0;
        if (recordCount < recordSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / recordSize);
            pageCount = ((int) flag);
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //子对象
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", jsonList);
        return result;
    }

    @Override
    public List<JSONObject> getBankDebitsByDownload(String companyId, String debitsNo, String comeProject, String dueDate,
                                                    String endorseFlag, String endorseDate, String key) {

        StringBuilder condition = new StringBuilder();
        List<Object> sqlArgs = new ArrayList<Object>();
        if (!StringUtils.isBlank(companyId)) {
            condition.append(" AND fkCompanyId=?");
            sqlArgs.add(companyId);
        }
        if (!StringUtils.isBlank(debitsNo)) {
            condition.append(" AND debitsNo=?");
            sqlArgs.add(debitsNo);
        }
        if (!StringUtils.isBlank(comeProject)) {
            condition.append(" and (incomeProject LIKE ?"
                    + " or outcomeProject LIKE ?)");
            sqlArgs.add("%" + comeProject + "%");
            sqlArgs.add("%" + comeProject + "%");
        }
        if (!StringUtils.isBlank(endorseFlag)) {
            condition.append(" AND endorseFlag=?");
            sqlArgs.add(endorseFlag);
        }
        if (!StringUtils.isBlank(dueDate)) {
            condition.append(" AND dueDate=?");
            sqlArgs.add(dueDate);
        }
        if (!StringUtils.isBlank(endorseDate)) {
            condition.append(" AND endorseDate=?");
            sqlArgs.add(endorseDate);
        }
        if (!StringUtils.isBlank(key)) {
            condition.append(" AND drawerName LIKE ?");
            sqlArgs.add("%" + key + "%");
        }
        condition.append(" AND delFlag=0");
        condition.append(" ORDER BY createTime DESC");
        return bankdebitsledgerDao.getViewJSONListByCondition(condition.toString(), sqlArgs.toArray());
    }

    @Override
    public List<JSONObject> getBankDebitsByReport(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        String dateTime = jsonParams.getString("date");
        float totalIncome = 0;
        float totaloutcome = 0;
        Integer number = 0;
        JSONObject result = new JSONObject();
        List<JSONObject> list = new ArrayList<>();
        List<JSONObject> inputList = bankdebitsledgerDao.getBankDebitsByInput(dateTime, companyId);
        for (int i = 0; i < inputList.size(); i++) {
            totalIncome += inputList.get(i).getFloat("money");
        }
        List<JSONObject> outputList = bankdebitsledgerDao.getBankDebitsByOutput(dateTime, companyId);
        for (int i = 0; i < outputList.size(); i++) {
            totaloutcome += outputList.get(i).getFloat("money");
        }
        if (inputList.size() > outputList.size()) {
            number = inputList.size();
        } else {
            number = outputList.size();
        }
        result.put("inputList", inputList);
        result.put("outputList", outputList);
        result.put("totalIncome", totalIncome);
        result.put("totaloutcome", totaloutcome);
        result.put("totalcount", totalIncome-totaloutcome);
        result.put("number", number);
        list.add(result);
        return list;
    }

}
